package com.hup.sokoban.view.customView.mapListView;

import com.hup.sokoban.model.common.MapItem;
import com.hup.utils.commons.DateBuilder;
import com.hup.utils.commons.hardware.SystemUtil;
import com.hup.utils.commons.start.LookThemeUtil;
import com.hup.utils.swing.ColorUtil;
import com.hup.utils.swing.ComponentUtil;
import lombok.extern.log4j.Log4j2;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.util.Date;

/**
 * 关卡预览窗口
 *
 * @author hugan
 * @date 2020/5/4
 */
@Log4j2
public class PreviewWindow extends JWindow {

    private PreviewWindowForm form;

    public PreviewWindow() {
        form = new PreviewWindowForm();
        form.root.setBorder(new LineBorder(ColorUtil.DEF_SELECTED_COLOR, 3));
        setContentPane(form.root);
        LookThemeUtil.fixFontScale(form.txtName);
        SystemUtil.disableInputMethods(this, true);
    }

    /**
     * 展示窗口,位置在当前窗口内,且不遮挡 mapListView
     */
    public void show(MapItem mapItem, MapListView mapListView) {
        log.debug("name={}, backgroundImage={}", mapItem.getName(), mapItem.getBackgroundImage());

        updateBounds(mapListView);

        form.txtName.setText(mapItem.getName());
        form.txtEditTime.setText(DateBuilder.ymdHms(new Date(mapItem.getLastModified())));
        if (mapItem.getDescription() == null) {
            form.txtDescription.setVisible(false);
        } else {
            form.txtDescription.setText(mapItem.getDescription());
            form.txtDescription.setVisible(true);
        }
        form.mapLabel.setData(mapItem);

        if (mapItem.isWinning()) {
            form.pWinning.setVisible(true);
            form.pPlaying.setVisible(false);
        } else if (mapItem.isPlaying()) {
            form.pWinning.setVisible(false);
            form.pPlaying.setVisible(true);
        } else {
            form.pWinning.setVisible(false);
            form.pPlaying.setVisible(false);
        }

        setVisible(true);
        //log.info("");
    }

    private void updateBounds(MapListView mapListView) {
        Window parentWindow = ComponentUtil.getParentWindow(mapListView);
        Dimension size = parentWindow.getSize();

        int paddingX = (int) (30 * LookThemeUtil.getScreenScale());
        int paddingY = size.height / 6;
        size.width -= (paddingX * 2 + mapListView.getWidth());
        size.height -= paddingY * 2;//高度是当前窗口的2/3

        int x = parentWindow.getX();
        int y = parentWindow.getY();

        x += (paddingX + mapListView.getWidth());
        y += paddingY;
        setBounds(x, y, size.width, size.height);
    }

}
